﻿using _9jCardRegisterCollection.Utils;
using CardRegisterCollection.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _9jCardRegisterCollection.Views
{
    public partial class CardTypeEdit : UserControl
    {
        public CardTypeEdit(AntdUI.Window _window, CardRegisterCollection.Model.CardType cardType1)
        {
            InitializeComponent();
            cardType = cardType1;
            window = _window;
            txtFatherCardType.Text = cardType.ParentId.ToString();
            txtCardName.Text = cardType.name?.ToString();
        }


        AntdUI.Window window;
        CardRegisterCollection.Model.CardType cardType;
        CardRegisterCollection.BLL.CardType cardTypeBll = new CardRegisterCollection.BLL.CardType();
        private void button_ok_Click(object sender, EventArgs e)
        {
            if (!Validation.CheckEmpty(window, this.txtCardName, "请输入证件名称")) { return; }
            cardType.name = this.txtCardName.Text;
           
            if (cardTypeBll.Exists($" and ID='{cardType.Id}'"))
            {
                cardType.LastUpdatedAt = DateTime.Now;
                cardType.LastUpdatedBy = CurrentLoginInfo.UserInfo.UserName;
                if (cardTypeBll.Update(cardType))
                {
                    AntdUI.Message.success(window, "操作成功", autoClose: 3);
                    this.Dispose();
                }
                else
                {
                    AntdUI.Message.error(window, "操作失败", autoClose: 3);
                }
            }
            else
            {
                if (cardTypeBll.Add(cardType))
                {
                    AntdUI.Message.success(window, "操作成功", autoClose: 3);
                    this.Dispose();
                }
                else
                {
                    AntdUI.Message.error(window, "操作失败", autoClose: 3);
                }
            }
        }

        private void CardTypeEdit_KeyPress(object sender, KeyPressEventArgs e)
        {
            return;
        }
    }
}
